package cn.icanci.loopstack.lbrt.core.utils;

import cn.icanci.loopstack.lbrt.core.exception.LbrtException;
import cn.icanci.loopstack.lbrt.core.model.LbrtConfigBean;
import cn.icanci.loopstack.lbrt.core.model.LbrtValue;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang3.reflect.FieldUtils;

/**
 * @author icanci
 * @since 1.0 Created in 2023/06/15 21:38
 */
public class PropertiesUtil {

    private static final String DEFAULT_LBRT_PATH = "lbrt.properties";
    /**
     * The constant confProperties.
     */
    private static Properties   confProperties;

    public static LbrtConfigBean assemblyLbrtBean(ClassLoader classLoader) {
        try {
            Map<String, String> lbrtPropertyMap = getLbrtPropertyMap(classLoader);
            LbrtConfigBean lbrtConfigBean = new LbrtConfigBean();
            Field[] fields = FieldUtils.getAllFields(LbrtConfigBean.class);
            for (Field field : fields) {
                LbrtValue anno = field.getAnnotation(LbrtValue.class);
                String value = anno.value();
                String propertyValue = lbrtPropertyMap.get(value);
                fieldPostProcessor(field.getType(), field, propertyValue, lbrtConfigBean);
            }
            return lbrtConfigBean;
        } catch (Exception e) {
            throw new LbrtException(e);
        }
    }

    /**
     * Get string.
     *
     * @param classLoader 类加载器
     * @return the string
     */
    private static Map<String, String> getLbrtPropertyMap(ClassLoader classLoader) {
        return getPropertyMap(classLoader, DEFAULT_LBRT_PATH);
    }

    /**
     * 初始化
     *
     * @param classLoader classLoader
     * @param sourcePath sourcePath
     */
    private static void init(ClassLoader classLoader, String sourcePath) {
        if (confProperties == null) {
            confProperties = new Properties();

            try (InputStream in = classLoader.getResourceAsStream(sourcePath)) {
                confProperties.load(in);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Get string.
     *
     * @param classLoader 类加载器
     * @return the string
     */
    private static Map<String, String> getPropertyMap(ClassLoader classLoader, String sourcePath) {
        // 初始化加载
        init(classLoader, sourcePath);

        Map<String, String> propertyMap = new HashMap<>();
        for (Map.Entry<Object, Object> next : confProperties.entrySet()) {
            propertyMap.put((String) next.getKey(), (String) next.getValue());
        }
        return propertyMap;
    }

    /**
     * 字段后置处理器
     * 
     * @param fieldClass fieldClass
     * @param field field
     * @param value value
     * @param bean bean
     * @throws IllegalAccessException IllegalAccessException
     */
    private static void fieldPostProcessor(Class<?> fieldClass, Field field, String value, Object bean) throws IllegalAccessException {
        field.setAccessible(true);
        // String 类型
        if (String.class.equals(fieldClass)) {
            field.set(bean, value);
            return;
        }
        // Long 类型
        if (Long.class.equals(fieldClass)) {
            field.set(bean, Long.parseLong(value));
            return;
        }
        // long 类型
        if (long.class.equals(fieldClass)) {
            field.set(bean, Long.parseLong(value));
            return;
        }
        // Double 类型
        if (Double.class.equals(fieldClass)) {
            field.set(bean, Double.parseDouble(value));
            return;
        }
        // double 类型
        if (double.class.equals(fieldClass)) {
            field.set(bean, Double.parseDouble(value));
            return;
        }
        // Integer 类型
        if (Integer.class.equals(fieldClass)) {
            field.set(bean, Integer.parseInt(value));
            return;
        }
        // int 类型
        if (int.class.equals(fieldClass)) {
            field.set(bean, Integer.parseInt(value));
            return;
        }
        // Float
        if (Float.class.equals(fieldClass)) {
            field.set(bean, Float.parseFloat(value));
            return;
        }
        // float
        if (float.class.equals(fieldClass)) {
            field.set(bean, Float.parseFloat(value));
            return;
        }
        // Boolean
        if (Boolean.class.equals(fieldClass)) {
            field.set(bean, Boolean.parseBoolean(value));
            return;
        }
        // boolean
        if (boolean.class.equals(fieldClass)) {
            field.set(bean, Boolean.parseBoolean(value));
            return;
        }
        // Character
        if (Character.class.equals(fieldClass)) {
            // Character 只取0
            field.set(bean, value.charAt(0));
            return;
        }
        // char
        if (char.class.equals(fieldClass)) {
            // Character 只取0
            field.set(bean, value.charAt(0));
            return;
        }
        // Short
        if (Short.class.equals(fieldClass)) {
            field.set(bean, Short.parseShort(value));
            return;
        }
        // short
        if (short.class.equals(fieldClass)) {
            field.set(bean, Short.parseShort(value));
            return;
        }
        // Byte
        if (Byte.class.equals(fieldClass)) {
            field.set(bean, Byte.parseByte(value));
            return;
        }
        // byte
        if (byte.class.equals(fieldClass)) {
            field.set(bean, Byte.parseByte(value));
            return;
        }
        // BigDecimal
        if (BigDecimal.class.equals(fieldClass)) {
            field.set(bean, new BigDecimal(value));
            return;
        }
        throw new IllegalAccessException("Not Support Class Type:" + fieldClass.getName());
    }
}
